﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using APICommon;
using APIComparer;

namespace APIManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void buildDatabaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (Ionic.Utils.FolderBrowserDialogEx fbd = new Ionic.Utils.FolderBrowserDialogEx())
            {
                fbd.Description = "Select a folder to extract to:";
                fbd.ShowNewFolderButton = true;
                fbd.ShowEditBox = true;
                //dlg1.NewStyle = false;
                fbd.ShowFullPathInEditBox = true;
                fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
                if (fbd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    textBox1.Clear();
                    await Task.Run(() => DataBuilder.Builder.Build(fbd.SelectedPath, ResultAction, ProgressAction));
                }
            }
        }

        private async void compareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (Ionic.Utils.FolderBrowserDialogEx fbd = new Ionic.Utils.FolderBrowserDialogEx())
            {
                fbd.Description = "Select a folder to extract to:";
                fbd.ShowNewFolderButton = true;
                fbd.ShowEditBox = true;
                //dlg1.NewStyle = false;
                fbd.ShowFullPathInEditBox = true;
                fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
                if (fbd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    textBox1.Clear();
                    await Task.Run(() =>  APIComparer.Comparer.Process(fbd.SelectedPath, ResultAction, ProgressAction));
                }
            }
        }

        private void ProgressAction(Progress progress)
        {
            this.Invoke((MethodInvoker)(()=>toolStripStatusLabel1.Text = progress.Message));
        }

        private void ResultAction(Result result)
        {
            this.Invoke((MethodInvoker)(() => textBox1.AppendText(String.Format("{0}\r\n", Path.GetFileName(result.Assembly)))));
            if (result != null)
            {
                if (result.HasErrors)
                {
                    foreach (var error in result.Errors)
                    {
                        this.Invoke((MethodInvoker)(() => textBox1.AppendText(error+"\r\n")));
                    }
                }

                if (result.ChangedMethods != null)
                {
                    this.Invoke((MethodInvoker)(() => textBox1.AppendText(string.Format("Changed methods:{0}\r\n", result.ChangedMethods.Count == 0 ? "None" : ""))));
                    foreach (var method in result.ChangedMethods)
                    {
                        this.Invoke((MethodInvoker)(() => textBox1.AppendText(String.Format("{0} v{1} changed to \r\n{2}\r\n", method.Item2.MethodSignature, method.Item1, method.Item3.MethodSignature))));
                    }
                }
                if (result.NewMethods != null)
                {
                    this.Invoke((MethodInvoker)(() => textBox1.AppendText(string.Format("New methods:{0}\r\n", result.NewMethods.Count == 0 ? "None" : ""))));
                    foreach (var method in result.NewMethods)
                    {
                        this.Invoke((MethodInvoker)(() => textBox1.AppendText(String.Format("Since v{0} {1}\r\n", method.Item1, method.Item2.MethodSignature))));
                    }
                }

                if (result.RemovedMethods != null)
                {
                    this.Invoke((MethodInvoker)(() => textBox1.AppendText(string.Format("Removed methods:{0}\r\n", result.RemovedMethods.Count == 0 ? "None" : ""))));
                    foreach (var method in result.RemovedMethods)
                    {
                        this.Invoke((MethodInvoker)(() => textBox1.AppendText(String.Format("Since {0} {1}\r\n", method.Item1, method.Item2.MethodSignature))));
                    }
                }
                
                this.Invoke((MethodInvoker)(() => textBox1.AppendText("\r\n")));
            }
        }
    }
}
